package net.xdclass.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.LoginRequest;
import net.xdclass.request.RegisterRequest;
import net.xdclass.service.FileService;
import net.xdclass.service.UserService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2022-11-01
 */
@RestController
@RequestMapping("api/user/v1")
public class UserController {
    @Autowired
    FileService fileService;
    @Autowired
    UserService userService;

    /**
     * 上传头像 springBoot默认最大是1M
     *
     * @param file
     * @return
     */
    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public JsonData uploadUserImage(
            @ApiParam(value = "文件上传", required = true)
            @RequestParam("file") MultipartFile file) {
        String result = fileService.uploadUserUmg(file);

        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMAGE_ERROR);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData register(@ApiParam("用户注册对象")@RequestBody RegisterRequest registerRequest) {
        JsonData reisger = userService.reisger(registerRequest);
        return reisger;
    }
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonData login(@ApiParam("用户登录对象")@RequestBody LoginRequest loginRequest){

        JsonData login =userService.login(loginRequest);
        return login;
    }

    @ApiOperation("个人信息查询")
    @GetMapping("/detil")
    public JsonData detil(){
     UserVo userVo= userService.selectUserDetil();
     return JsonData.buildSuccess(userVo);
    }

}

